var curFiles = [];
var downUrl = [];
var title;
function checkyes(id) {
    var tianan;
    $.ajax({
        type: "GET",
        url: baseURL + "../sys/tianan/info/" + id,
        cache: false,
        async: false,
        success: function (r) {
            tianan = r.tianan;
            var lianstatu;
            if (tianan.lianstatus == 2||tianan.lianstatus == '2') {
                lianstatu = checkstatus(tianan.banjiestatus)
            } else {
                lianstatu = lianstatus(tianan.lianstatus)

            }
            if(tianan.liantype==3){

                lianstatu = '<span style="color: red">终止</span>';
            }
            if (tianan.banjiestatus<5) {
                tianan.subexplain='';
                tianan.subfile='';

            }

            layer.open({
                title: tianan.title,
                type: 1,
                skin: 'layui-layer-molv',
                area: ['800px', '450px'],
                btn: ['确定'],
                content: '<div class="row" style="padding: 10px">' +
                    ' <table class="table table-bordered table-style"> ' +
                    ' <tbody> ' +
                    '<tr><td style="width:110px; ">提案人</td><td style="    width: 140px;">' + isnull(tianan.creatusername) + '</td><td style="width:85px;text-align: center; ">提案类别</td><td>' + isnull(tianan.typesName) + '</td><td style="width:85px;text-align: center; ">所在部门</td><td>' + isnull(tianan.deptIdName) + '</td></tr>' +
                    '<tr><td>案由分析</td><td colspan="5">' + isnull(tianan.anyoufenxi) + '</td></tr>' +
                    '<tr><td>措施建议</td><td colspan="5">' + isnull(tianan.cuoshijianyi) + '</td></tr>' +
                    '<tr><td>办理时限</td><td >' + isnull(tianan.endtime) + '</td><td style="width:85px;text-align: center;">办理类型</td><td>' + isnull(liantype(tianan.liantype)) + '</td><td style="width:85px;text-align: center; ">办理部门</td><td>' + isnull(tianan.liandeptname) + '</td></tr>' +
                    '<tr><td>提案类型</td><td >' + isnull(tianleibie(tianan.type)) + '</td><td style="width:85px;text-align: center;">办理状态</td><td>' + lianstatu + '</td><td style="width:85px;text-align: center; ">提案人满意度</td><td>' + isnull(manyistatus(tianan.manyi)) + '</td></tr>' +
                    '<tr><td>提案答复</td><td colspan="5">' + isnull(tianan.subexplain) + '</td></tr>' +
                    '<tr><td>部门负责人审核时间</td><td colspan="2">' + isnull(tianan.restime) + '</td><td>分管院领导审核时间</td><td colspan="2">' + isnull(tianan.ledtime) + '</td></tr>' +
                    ' </tbody> ' +
                    '</table>' +
                    '<div class="input-group ">相关附件：<ul id="ulli"></ul></div>' +
                    '<h3 style="text-align: center; margin-top: 20px;">签批意见</h3>'+
                    '<div style="line-height: 35px">' +
                    '<input type="radio" name="tongyi" style="margin: 0 auto;"  checked value="1"/>&nbsp;同意&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '<input type="radio" name="tongyi" style="margin: 0 auto;"  value="3"/>&nbsp;不同意&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '<input type="radio" name="tongyi" style="margin: 0 auto;"  value="2"/>&nbsp;其它意见&nbsp;&nbsp;&nbsp;&nbsp;' +
                    '</div>'+
                    '<textarea  style="margin: 10px;" name=""  placeholder="签批意见 " class="layui-textarea  liancause"></textarea>' +
                    ' </div>',
                success: function () {

                    if (tianan.subfile != null && tianan.subfile != '') {
                        var s = [];
                        s = tianan.subfile.split(",");
                        //遍历所有文件
                        for (var i = 0; i < s.length; i++) {
                            if (s[i] != null && s[i] != 'null') {
                                s[i].replace("/", "\\")
                            }
                            var fileNameIndex = s[i].lastIndexOf("\\");
                            var filePathName = s[i].substring(fileNameIndex + 1);
                            var html = filePathName;
                            downUrl.push(s[i]);

                            html = '<li>' +
                                html + '<a class="operation_type" style="color: #0c74c4; text-decoration:none" href="../../../sys/file/fileDown?fileDir=' + encodeURIComponent(s[i]) + '"  > &nbsp;&nbsp;下载</a>'
                                + "<a class='operation_type'  style='color: #0c74c4;  border:none;text-decoration:none'   onclick='eye( \"" + s[i].replace(/\\/g, "//") + "\")' href='javascript:;'  > &nbsp;&nbsp;预览</a></li>";
                            $("#ulli").append(html);
                        }
                    } else {
                        $("#ulli").append("<p>无</p>");
                    }
                }  ,btn1: function () {
                    vm.tianan.id=tianan.id;
                    vm.tianan.tongyi=  $("input[name='tongyi']:checked").val();
                    vm.tianan.yijian = $(".liancause").val();

                    var url = "sys/tianan/updateyushen";
                    $.ajax({
                        type: "POST",
                        url: baseURL + url,
                        contentType: "application/json",
                        data: JSON.stringify(vm.tianan),
                        success: function (r) {
                            if (r.code === 0) {
                                layer.msg("签发成功", {icon: 1});
                                vm.reload();
                            } else {
                                layer.alert(r.msg);

                            }
                        }
                    });
                    layer.closeAll()
                },

            })
        }
    })

}


function init() {

    $("#jqGridtable").html("");
    $("#jqGridtable").html('<table id="jqGrid"></table><div id="jqGridPager"></div>');
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/tianan/listqianpi?' +
            'sea=' + $("#sea").val() + '&types=' + $("#types").val() + '&tongyi=' + $("#tongyi").val() + '&manyi=' + $("#manyi").val() + '&liandept=' + $("#liandept").val()+ '&year=' +$('#listyear').val(),
        datatype: "json",
        colModel: [
            {label: '提案名称', name: 'title', index: 'title', width: 60},

            {
                label: '立案类型',
                align: 'center',
                sortable: true,
                name: 'liantype',
                index: 'liantype',
                width: 40,
                formatter: function (value, row, rowObject) {

                    return lian(value);
                }
            }, {
                label: '标签',
                align: 'center',
                sortable: true,
                name: 'lable',
                index: 'lable',
                width: 40,
            },  {
                label: '办理时限',
                name: 'endtime',
                index: 'endtime',
                align: 'center',
                width: 40,},
            {
                label: '状态',

                name: 'lianstatus',
                index: 'lianstatus',
                align: 'center',
                width: 40,
                formatter: function (value, row, rowObject) {

                    if (rowObject.lianstatus != 2) {
                        return lianstatus(rowObject.lianstatus);
                    } else {
                        return checkstatus(rowObject.banjiestatus);
                    }


                }
            },
            {label: '提案人', align: 'center', sortable: true, name: 'creatusername', index: 'dept_id', width: 40},
            {
                label: '操作',
                name: 'id',
                align: 'center',
                index: 'id',
                width: 40,
                formatter: function (value, row, rowObject) {
                    var but = "";
                    but += ' <button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="info(' + value + ')">详情</button>';

                    if (rowObject.tongyi == null) {
                        but += '<button type="button" class="layui-btn layui-btn-xs layui-btn-normal" onclick="checkyes(' + value + ',' + rowObject.id + ')">签批</button>';
                    }
                    return but;
                }
            }


        ],
        viewrecords: true,
        height: 360,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
}

$(function () {
    init();
    $('#tongyi').selectpicker({
        width: '100px'
    });
    $('#manyi').selectpicker({
        width: '100px'
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        tiananShenyi: {},
        listyear:inityear(),
        deptlist: getDeptAll(''),
        dictlist: getDict('tiantype'),
        tianan: {}
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.tiananShenyi = {};
        },
        update: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id)
        },
        saveOrUpdate: function (event) {
            $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function () {
                var url = vm.tiananShenyi.id == null ? "sys/tiananshenyi/save" : "sys/tiananshenyi/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(vm.tiananShenyi),
                    success: function (r) {
                        if (r.code === 0) {
                            layer.msg("操作成功", {icon: 1});
                            vm.reload();
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        } else {
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
            });
        },
        del: function (event) {
            var ids = getSelectedRows();
            if (ids == null) {
                return;
            }
            var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                if (!lock) {
                    lock = true;
                    $.ajax({
                        type: "POST",
                        url: baseURL + "sys/tiananshenyi/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function (r) {
                            if (r.code == 0) {
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            } else {
                                layer.alert(r.msg);
                            }
                        }
                    });
                }
            }, function () {
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "sys/tiananshenyi/info/" + id, function (r) {
                vm.tiananShenyi = r.tiananShenyi;
            });
        },
        gettiananInfo: function (id) {

        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                page: page
            }).trigger("reloadGrid");
        }
    }
});
